package BlogMybatis.blog.controller.admin;


import BlogMybatis.blog.pojo.Tag;
import BlogMybatis.blog.service.TagService;
import BlogMybatis.blog.service.TypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Brandt
 * @since 2020-06-07
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    //后台标签列表
    @GetMapping("/tags")
    public String tags(@RequestParam(required = false,defaultValue = "1") int pageNum, Model model){
        PageHelper.startPage(pageNum, 5);
        List<Tag> listTag = tagService.list();
        PageInfo<Tag> pageInfo = new PageInfo<>(listTag);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/tags";
    }

    //跳到添加标签页
    @GetMapping("/tags/input")
    public String toAddTag(Model model){
        Tag tag = new Tag();
        model.addAttribute("tag",tag);
        return "admin/tags-input";
    }

    //新增标签名称
    @PostMapping("/tags")
    public String addTag(Tag tag, RedirectAttributes attributes){
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name",tag.getName());
        Tag t = tagService.getOne(wrapper);
        if(t != null){
            attributes.addFlashAttribute("message","不能添加重复的标签名称");
            return "redirect:/admin/tags/input";
        }else {
            attributes.addFlashAttribute("message","添加成功");
        }
        tagService.save(tag);
        return"redirect:/admin/tags";
    }

    //根据id跳到编辑页面
    @GetMapping("/tags/{id}/input")
    public String toEditTag(@PathVariable Long id, Model model){
        model.addAttribute("tag",tagService.getById(id));
        return "admin/tags-input";
    }

    //修改标签
    @PostMapping("/tags/{id}")
    public String editTag(@PathVariable Long id, Tag tag, RedirectAttributes attributes){
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name",tag.getName());
        Tag t = tagService.getOne(wrapper);
        if(t != null){
            attributes.addFlashAttribute("message","该标签名称已存在，不能重复添加");
            return "redirect:/admin/tags/{id}/input";
        }else {
            attributes.addFlashAttribute("message","修改成功");
        }
        tagService.updateById(tag);
        return "redirect:/admin/tags";
    }

    //删除标签
    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable Long id,RedirectAttributes attributes){
        tagService.removeById(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";
    }
}

